<?php
namespace app\common\model;
use app\common\model\User as UserModel;

class UserRz extends Common
{
    /**
     * 添加更新数据
     * @param $data
     * @return array
     */
    public function createRz($data){
        $rz=self::where('uid',$data['uid'])->find();
        if($rz){
            $rz->realname=$data['realname'];
            $rz->idno=$data['idno'];
            $rz->f_url=$data['f_url'];
            $rz->b_url=$data['b_url'];
            $rz->status=0;
            $rz->ctime=$data['ctime'];
            $rz->save();
        }else{
            $res=self::create($data,true);
        }
        if(!$res){
            return [false,'认证提交失败'];
        }
        return [true,'认证提交成功'];
    }
    protected function tableWhere($post)
    {
        $where = [];
        if(isset($post['user_id']) && $post['user_id'] != ""){
            $where[] = ['user_id', 'eq', $post['user_id']];
        }
        if (isset($post['id']) && $post['id'] != "") {
            $where[] = ['id', 'eq', $post['id']];
        }
        if(!empty($post['date']))
        {
            $date_string = $post['date'];
            $date_array = explode(' 到 ', $date_string);
            $sdate = strtotime($date_array[0].' 00:00:00');
            $edate = strtotime($date_array[1].' 23:59:59');
            $where[] = array('ctime', ['>=', $sdate], ['<', $edate], 'and');
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = "ctime desc";
        return $result;
    }
    /**
     * 根据查询结果，格式化数据
     * @author sin
     * @param $list  array格式的collection
     * @return mixed
     */
    protected function tableFormat($list)
    {
        foreach($list as $k => $v) {
            $list[$k]['state'] = config('params.user')['state'][$v['state']];
            $list[$k]['ctime'] = getTime($v['ctime']);
        }
        return $list;
    }


    /**
     * 返回layui的table所需要的格式
     * @author sin
     * @param $post
     * @return mixed
     */
    public function tableDatas($post, $isPage = true)
    {
        if (isset($post['limit'])) {
            $limit = $post['limit'];
        } else {
            $limit = config('paginate.list_rows');
        }
        $tableWhere = $this->tableWheres($post);
        $list       = [];
        if ($isPage) {
            $list        = $this->with('grade')->field($tableWhere['field'])->where($tableWhere['where'])->order($tableWhere['order'])->paginate($limit);
            $data        = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = $list->total();
        } else {
            $list = $this->field($tableWhere['field'])->where($tableWhere['where'])->order($tableWhere['order'])->select();
            if (!$list->isEmpty()) {
                $data = $this->tableFormat($list->toArray());
            }
            $re['count'] = count($list);
        }
        $userModel = new User();
        foreach ($data as &$v)
        {
            $userInfo = $userModel->field('id,username,nickname,mobile')->where(['id' => $v['user_id']])->select();
            $v['username'] = $userInfo[0]['mobile'];
        }
        $re['code'] = 0;
        $re['msg']  = '';

        $re['data'] = $data;

        return $re;
    }

    protected function tableWheres($post)
    {
        $where = [];
        if (isset($post['id']) && $post['id'] != "") {
            $where[] = ['id', 'in', $post['id']];
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = "id desc";
        return $result;
    }
    /**
     * 时间范围查询
     * @param  [type] $query [description]
     * @param  [type] $value [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function searchCtimeAttr($query,$value, $data)
    {
        $start=$value[0]?strtotime($value[0]):0;
        $end=$value[1]?strtotime($value[1]):0;
        if(!empty($start)&&!empty($end)){
            $query->where('ctime','between',[$start,$end]);
        }elseif(!empty($start)&&empty($end)){
            $query->where('ctime','>',$start);
        }elseif(empty($start)&&!empty($end)){
            $query->where('ctime','<',$end);
        }
    }
    /**
     * 状态查询
     * @param  [type] $query [description]
     * @param  [type] $value [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function searchStatusAttr($query,$value, $data)
    {
        if(strlen($value)){
            $query->where('status',$value);
        }       
    }
    /**
     * 关键字查询
     * @param  [type] $query [description]
     * @param  [type] $value [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function searchNameAttr($query,$value, $data)
    {
        if(!empty($value)){
            $query->where('uid|realname',$value);
        }
    }
}